// eee.Sheffield.PZ.Imaging
//
// Copyright ?Ping Zou, 2007
// sg71.cherub@gmail.com

using System;
using System.Collections.Generic;
using System.Text;
using eee.Sheffield.PZ.Math;
using System.Drawing;
using System.Drawing.Imaging;

namespace eee.Sheffield.PZ.Imaging.ImageFilter
{
    public class TaskGetRemovedLineSegments
    {
        private string _allLineSegmentsImageFileName;
        private string _manualLineSegmentsImageFileName;

        public TaskGetRemovedLineSegments(string all, string manual)
        {
            _allLineSegmentsImageFileName = all;
            _manualLineSegmentsImageFileName = manual;
        }

        public List<LineSegment> GetRemovedLineSegments()
        {
            ConvertBitmapMatrix converter = new ConvertBitmapMatrix();
            FreeEndDetector freeEndDetector = new FreeEndDetector();
            TripleJunctionDetector junctionDetector = new TripleJunctionDetector();
            LineSegmentSplitter splitterAll;
            LineSegmentSplitter splitterManual;

            // read all line segments image
            Bitmap allLineSegmentsImage = new Bitmap(_allLineSegmentsImageFileName);
            eee.Sheffield.PZ.Imaging.Image.StandardizeImageTo8bbpIndexed(ref allLineSegmentsImage);
            //PZMath_matrix allLineSegmentMatrix = converter.ConvertGraylevelBitmapToMatrix(allLineSegmentsImage);
            freeEndDetector.ApplyGraylevelImageFilter(allLineSegmentsImage);
            junctionDetector.ApplyGraylevelImageFilter(allLineSegmentsImage);
            splitterAll = new LineSegmentSplitter(freeEndDetector.FreeEndList, junctionDetector.TripleJunctionList);
            splitterAll.ApplyGraylevelImageFilter(allLineSegmentsImage);


            // read manual line segments image
            Bitmap manualLineSegmentsImage = new Bitmap(_manualLineSegmentsImageFileName);            
            eee.Sheffield.PZ.Imaging.Image.StandardizeImageTo8bbpIndexed(ref manualLineSegmentsImage);
            freeEndDetector.ApplyGraylevelImageFilter(manualLineSegmentsImage);
            junctionDetector.ApplyGraylevelImageFilter(manualLineSegmentsImage);
            splitterManual = new LineSegmentSplitter(freeEndDetector.FreeEndList, junctionDetector.TripleJunctionList);
            splitterManual.ApplyGraylevelImageFilter(manualLineSegmentsImage);
            
            // remove manual from all
            int allLineSegment = splitterAll.LineSegmentList.Count;
            int index = 0;
            while (index < allLineSegment)
            {
                LineSegment l = (LineSegment)splitterAll.LineSegmentList[index];
                bool toRemove = false;
                foreach (LineSegment m in splitterManual.LineSegmentList)
                {
                    if (l.IsPartOf(m))
                    {
                        toRemove = true;
                        break;
                    }
                }

                if (toRemove)
                {
                    splitterAll.LineSegmentList.RemoveAt(index);
                    allLineSegment--;
                }
                else
                {
                    index++;
                }
            }

            return splitterAll.LineSegmentList;
        } // GetRemovedLineSegments()
    }
}
